﻿#pragma once

#include <QUdpSocket>
#include <QThread>
#include <QTimer>
#include <QMutexLocker>
#include <QHostAddress>
#include <QDateTime>
#include <QRandomGenerator>
#include <memory>

#include "PlayerGameData.h"
#include "MonsterData.h"
#include "GamePacket.h"
#include "NetworkWorker.h"
#include "UniversalProtocol.h"
#include "ProtocolAdapters.h"

class GameServer : public QObject
{
    Q_OBJECT
    
public:
    explicit GameServer(QObject *parent = nullptr);
    ~GameServer();
    
    // 静态调试开关 - 设为true开启调试，false关闭调试
    static bool s_debugMode;
    
    bool startServer(quint16 port = 12345);
    void stopServer();
    
    bool isRunning() const 
    { 
        return m_isRunning; 
    }
    
    // 玩家管理
    void addPlayer(quint32 playerId, const QString& name, const QHostAddress& address, quint16 port);
    void removePlayer(quint32 playerId, bool isVoluntary = false);
    PlayerInfo* getPlayer(quint32 playerId);
    QList<PlayerInfo> getAllPlayers() const;
    
    // 数据发送
    void sendToPlayer(quint32 playerId, std::shared_ptr<GamePacket> packet);
    void sendRawData(const QByteArray& data, const QHostAddress& target, quint16 port);
    
    // 新增：通用协议数据发送
    void sendUniversalMessage(quint32 playerId, std::shared_ptr<UniversalProtocol::UniversalMessage> message);
    void sendRawUniversalData(const std::vector<uint8_t>& data, const QHostAddress& target, quint16 port);

public slots:
    void onReceiveMessageEvent();
    void onPacketProcessed(std::shared_ptr<GamePacket> packet, const QHostAddress& sender, quint16 port);
    
    // 新增：通用协议消息处理
    void onUniversalMessageProcessed(std::shared_ptr<UniversalProtocol::UniversalMessage> message, const QHostAddress& sender, quint16 port);

private slots:
    void handlePlayerJoin(std::shared_ptr<GamePacket> packet, const QHostAddress& sender, quint16 port);
    void handlePlayerLeave(std::shared_ptr<GamePacket> packet, const QHostAddress& sender, quint16 port);
    void handleGameStart(std::shared_ptr<GamePacket> packet, const QHostAddress& sender, quint16 port);
    void handleAttack(std::shared_ptr<GamePacket> packet, const QHostAddress& sender, quint16 port);
    
    // 新增：通用协议消息处理槽
    void handleUniversalPlayerJoin(std::shared_ptr<UniversalProtocol::UniversalMessage> message, const QHostAddress& sender, quint16 port);
    void handleUniversalPlayerLeave(std::shared_ptr<UniversalProtocol::UniversalMessage> message, const QHostAddress& sender, quint16 port);
    void handleUniversalGameStart(std::shared_ptr<UniversalProtocol::UniversalMessage> message, const QHostAddress& sender, quint16 port);
    void handleUniversalAttack(std::shared_ptr<UniversalProtocol::UniversalMessage> message, const QHostAddress& sender, quint16 port);
    
    // 新增：自动战斗系统
    void processCombatRound(quint32 playerId);

private:
    // 基础服务器功能
    quint32 generatePlayerId();
    
    // MUD游戏逻辑
    void startNewGame(quint32 playerId);
    void generateNewMonster(quint32 playerId);
    void processAttack(quint32 playerId);  // 修改为启动自动战斗
    void sendGameStatus(quint32 playerId, const QString& message = "");
    
    // 新增：自动战斗系统
    void startAutoCombat(quint32 playerId);
    void stopAutoCombat(quint32 playerId);
    void sendCombatMessage(quint32 playerId, const QString& message);
    QTimer* getCombatTimer(quint32 playerId);
    
    // 新增：通用协议处理方法
    void handleProtocolHandshake(std::shared_ptr<UniversalProtocol::UniversalMessage> message, const QHostAddress& sender, quint16 port);
    void sendUniversalCombatMessage(quint32 playerId, const std::string& message);

private:
    QUdpSocket* m_receiver;
    QThread* m_workerThread;
    NetworkWorker* m_worker;
    
    QMap<quint32, PlayerInfo> m_players;
    QMutex m_playersMutex;
    
    // 新增：战斗计时器管理
    QMap<quint32, QTimer*> m_combatTimers;
    QMutex m_timersMutex;
    
    // 战斗状态管理（线程安全的成员变量，替代静态变量）
    QMap<quint32, int> m_roundCounters;  // 每个玩家的回合计数器
    
    // 新增：协议适配器和客户端类型管理
    QMap<quint32, UniversalProtocol::ClientType> m_clientTypes;  // 玩家ID到客户端类型的映射
    QMap<QString, UniversalProtocol::ClientType> m_addressToClientType;  // 客户端地址到类型的映射
    QMutex m_clientTypesMutex;  // 客户端类型映射保护锁
    
    bool m_isRunning;
    quint16 m_port;
    quint32 m_nextPlayerId;
};

